import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import admin from '@/views/layout/admin'
// 解决两次访问相同路由地址报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
const router = new Router({
  //authorization: true 参数区别是否需要登录
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login')
    },
    //为layout下的子组件
    {
      path: '/',
      name: 'Home',
      component: admin,
      children:[
        {
          path: '/home',
          name: 'Home',
          component: () => import('@/views/home')
        },
        {
          path: '/order',
          name: 'Order',
          // meta: { authorization: true },
          component: () => import('@/views/order')
        },
        {
          path: '/user',
          name: 'User',
          meta: { authorization: true },
          component: () => import('@/views/user')
        }
      ]
    },
  ]

})
//路由拦截，可根据实际需求进行变更
const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  //NProgress.start()
  if (to.meta&&to.meta.authorization&&!store.getters.token&&whiteList.indexOf(to.path) == -1) {//需要验证权限并未登陆
    store.commit('SET_REDIRECTROUTE', { name: to.name, query: to.query })
    next({ name: 'Login' })
  } else {
    next()  
  }
  
})

router.afterEach(() => {
  //NProgress.done() // 结束Progress
})
export default router

